package com.mengmablog.blog.commons;

import lombok.Data;

/**
 * @Auther: Tangzhiqiang
 * @Date: 2018/12/26 15:02
 * @Description: 高可用复用性对象
 */
@Data
public class ServerResponse<T> {

    private int code; // 0
    private String msg;
    private boolean success; // 默认值为 false
    private T data;


    public ServerResponse(int code, boolean success) {
        this.code = code;
        this.success = success;
    }

    private ServerResponse(int code, boolean success, T data) {
        this.success = success;
        this.code = code;
        this.data = data;
    }

    private ServerResponse(int code, T data) {
        this.code = code;
        this.data = data;
    }

    private ServerResponse(int code, boolean success, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private ServerResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private ServerResponse(int code, boolean success, String msg) {
        this.success = success;
        this.code = code;
        this.msg = msg;
    }

    private ServerResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    // 判断是否成功
    public boolean isSuccess() {
        return this.success == true;
    }


    /**
     * 成功时调用此方法，不传递任何值
     */
    public static <T> ServerResponse<T> createBySuccess() {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), true);
    }


    /**
     * 成功时调用此方法，并且传递自定义 msg 值
     */
    public static <T> ServerResponse<T> createBySuccessMessage(String msg) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), true, msg);
    }

    /**
     * 成功时调用此方法，传递data(T) 数据给调用端
     */
    public static <T> ServerResponse<T> createBySuccess(T data) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), true, data);
    }

    public static <T> ServerResponse<T> createBySuccess(String msg, T data) {
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), true, msg, data);
    }

    public static <T> ServerResponse<T> createByError() {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg());
    }

    public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> ServerResponse<T> createByErrorcodeMessage(int errorCode, String errorMessage) {
        return new ServerResponse<T>(errorCode, errorMessage);
    }

}
